/* Author: Mark Mckillion, Chris Mowbray
 * Date: 17/04/2012
 * Version 1.0.*/

/* This is the Selection Sorting Method it works by first find the smallest in the 
 * array and exchange it with the element in the first position, then find the second 
 * smallest element and exchange it with the element in the second position, and continue 
 * in this way until the entire array is sorted.
 */
package assignment4;

import java.awt.Color;
import java.awt.Image;
import javax.swing.text.Element;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class SelectionSort {

    // as mentioned other classes we get the current text from the textpane and
    // create a styled document so be edited
    static StyledDocument sdoc = ModelFrame.algoritm.getStyledDocument();
    static SimpleAttributeSet attrs = new SimpleAttributeSet();
    // int that i sused to sleep the current thread, updated by the slider
    public static int speed;
    // get the paragraph
    static Element section = ModelFrame.algoritm.getDocument().getDefaultRootElement();
    // get the number of lines
    int paraCount = section.getElementCount();
    
    
     // the selection sort algorithm with 'Thread.sleep' throughout so that this sorting
    // algoritm will be slowed down.
    // for the colurs
    public static void sortObjects(Color[] list) {
        list = Colour.colors;
        int min;
        Color temp;
        // highligh line 3
        highlight(3);
        // sleep for period of time
        try {
            Thread.sleep(speed);
        } catch (Exception e) {
        }
        //dehighligh line 3
        deHighlight(3);
        for (int index = 0; index < list.length - 1; index++) {
                       
            highlight(5);
            try {
                Thread.sleep(speed);
            } catch (Exception e) {
            }
            deHighlight(5);
            min = index;

            highlight(6);
            try {
                Thread.sleep(speed);
            } catch (Exception e) {
            }
            deHighlight(6);
            for (int scan = index + 1; scan < list.length; scan++) {
                            
            DrawingPanel.comp1 = index;
            DrawingPanel.comp2 = scan;
            DrawingPanel.sn = index;
            ModelFrame.drawing.repaint();
                
                highlight(8);
                try {
                    Thread.sleep(speed);
                } catch (Exception e) {
                }
                
                deHighlight(8);
                if (Colour.colour.compareTo(list[scan], list[min]) == -1) {
                    highlight(10);
                    try {
                        Thread.sleep(speed);
                    } catch (Exception e) {
                    }
                    deHighlight(10);
                    min = scan; 
                }
            }
            

            highlight(13);
            try {
                Thread.sleep(speed);
            } catch (Exception e) {
            }
            deHighlight(13);
            temp = list[min];
            
            DrawingPanel.comp2 = min;
            ModelFrame.drawing.repaint();
            
            highlight(14);
            try {
                Thread.sleep(speed);
            } catch (Exception e) {
            }
            deHighlight(14);
            list[min] = list[index];
            highlight(15);
            try {
                Thread.sleep(speed);
            } catch (Exception e) {
            }
            deHighlight(15);
            list[index] = temp;
            ModelFrame.drawing.repaint();            
        }   
            DrawingPanel.sn = list.length;
            ModelFrame.drawing.repaint();
        highlight(17);
            
    }
        
    // same sorting method but ofr the vehicles
        public static void sortObjects(Image [] list) {
        list = Vehicle.vehicles;
        int min;
        Image temp;

        highlight(3);
        try {
            Thread.sleep(speed);
        } catch (Exception e) {
        }
        deHighlight(3);
        for (int index = 0; index < list.length - 1; index++) {
                       
            highlight(5);
            try {
                Thread.sleep(speed);
            } catch (Exception e) {
            }
            deHighlight(5);
            min = index;

            highlight(6);
            try {
                Thread.sleep(speed);
            } catch (Exception e) {
            }
            deHighlight(6);
            for (int scan = index + 1; scan < list.length; scan++) {
                            
            DrawingPanel.comp1 = index;
            DrawingPanel.comp2 = scan;
            DrawingPanel.sn = index;
            ModelFrame.drawing.repaint();
                
                highlight(8);
                try {
                    Thread.sleep(speed);
                } catch (Exception e) {
                }
                
                deHighlight(8);
                if (Vehicle.vehicle.compareTo(list[scan], list[min]) == -1) {
                    highlight(10);
                    try {
                        Thread.sleep(speed);
                    } catch (Exception e) {
                    }
                    deHighlight(10);
                    min = scan; 
                }
            }
            

            highlight(13);
            try {
                Thread.sleep(speed);
            } catch (Exception e) {
            }
            deHighlight(13);
            temp = list[min];
            
            DrawingPanel.comp2 = min;
            ModelFrame.drawing.repaint();
            
            highlight(14);
            try {
                Thread.sleep(speed);
            } catch (Exception e) {
            }
            deHighlight(14);
            list[min] = list[index];
            highlight(15);
            try {
                Thread.sleep(speed);
            } catch (Exception e) {
            }
            deHighlight(15);
            list[index] = temp;
            ModelFrame.drawing.repaint();            
        }   
            DrawingPanel.sn = list.length;
            ModelFrame.drawing.repaint();
        highlight(17);
            
    }
        // this is a method to highlight the current line that the thread is currently on
        // so that the user can follow what is happening.
    public static void highlight(int i) {
        // setting the current line as an element
        Element e = section.getElement(i);
        // getting start of the line.
        int rangeStart = e.getStartOffset();
        // getting end of the line
        int rangeEnd = e.getEndOffset();

        //these 3 lines together slect the line from start to end and highligh using
        // the colour gray.
        sdoc.setCharacterAttributes(rangeStart, (rangeEnd - rangeStart), attrs, false);
        StyleConstants.setBackground(attrs, Color.LIGHT_GRAY); // change to white
        sdoc.setCharacterAttributes(rangeStart, (rangeEnd - rangeStart), attrs, false);
    }

        // the deHighlight method is initially the same as the highlight method but it 
    //changes the background to white to give the illusion of it being highlighted.
    public static void deHighlight(int i) {
        Element e = section.getElement(i);
        int rangeStart = e.getStartOffset();
        int rangeEnd = e.getEndOffset();

        sdoc.setCharacterAttributes(rangeStart, (rangeEnd - rangeStart), attrs, false);
        StyleConstants.setBackground(attrs, Color.WHITE); // change to white
        sdoc.setCharacterAttributes(rangeStart, (rangeEnd - rangeStart), attrs, false);
    }
    
     // method that is called in the jsldier actionlistner to get the current speed
    // of the threads by way to sleeping the thread.
    public static void getV(int v)
    {
        int i = v * 75;
        speed = 800 - i;
    }
}   //End of SelectionSort class.